<?php
/**
 *
 *----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * Created by PhpStorm.
 * User: huangxh
 * Date: 2018/1/5 0005
 * Time: 下午 12:01
 */

use models\DAO\GroupModel;
use Yaf\Controller_Abstract;
use Common\Auth;
use Common\Common;
use Yaf\Registry;
use Yaf\Exception;
use Yaf\Session;

class BaseController extends Controller_Abstract
{
    protected function init() {

        $notAuthenticationList = explode(',',Registry::get('config')['application']['notauthentication']['Controller']);
        $module = $this->getRequest()->getModuleName();
        $controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        if (!in_array($controller,$notAuthenticationList)){
            $session_auth = Session::getInstance()->get(session_id());

        if (!isset($session_auth)) {

            $this->redirect('/admin/login/index');
            return;
        }

            if ($session_auth['role'] == 1) {return TRUE;}
            $auth = new Auth();
            if (!$auth->check(strtolower($module . '/' . $controller . '/' . $action), $session_auth['id'])) {
                throw new Exception('没有权限', 000);
            }
        }

    }

    /**
     * @param array $data
     * @param bool $flag        返回格式的开关，为true 为标准的格式带code、msg
     * @param string $code
     * @param string $msg
     */
    public function returnJson($data = [],$flag = true,$code = 1,$msg = ''){
        header('content-type:application/json');
        if ($flag)
            $this->getResponse()->setBody(json_encode(['data' =>$data,'code' => $code, 'msg' => $msg]));
        else
            $this->getResponse()->setBody(json_encode($data));
    }

    public function Json($content =['code'=>0,'msg'=>'']){
        $this->getResponse()->setBody(json_encode(['code'=>$content[0],'msg'=>$content[1]]));
    }

    //头文件
    protected function _setHeader(){
        $this->getView()->assign('title', '管理后台');
        $this->getView()->display('header.phtml');
    }

    //尾文件
    protected function _setFooter(){
        $this->getView()->display('footer.phtml');
    }

    //左侧菜单栏
    protected function _setLeftMenu(){

        if (empty(Session::getInstance()->get(session_id()))){
            throw new Exception('没有权限', 000);
        }
        $data =[
            'username' => Session::getInstance()->get(session_id())['username'],
            'role' => Session::getInstance()->get(session_id())['rolename']
        ];
        $this->getView()->data = $data;
        $redisRules = Registry::get('rdm')->getString('menu:'.session_id().':rule');

        $ruleModel =new \models\DAO\RuleModel();
        if (empty($redisRules)){
            //redis为空，第一次登陆
            $group = GroupModel::getInstance()->find(['id'=>Session::getInstance()->get(session_id())['role']]);
            $map=array(
                'display'=>1,
                'status'=>1,
            );
            if (!empty($group->_rules)){
                $map=array(
                    'id'=>['in'=>$group->_rules]
                );
            }
            //读取用户组所有权限规则
            $rules = $ruleModel->where($map)->select();
            $menu = Common::getTree($rules->data);
        }else{
            if (is_array($redisRules)){
                $menu = $redisRules;
            }else{
                $map=array(
                    'display'=>1,
                    'id'=>['in'=>$redisRules],
                    'status'=>1,
                );

                //读取用户组所有权限规则

                $rules = $ruleModel->where($map)->select();
                $menu = Common::getTree($rules->data);



            }
        }


        Registry::get('rdm')->genString('menu:'.session_id().':rule',$menu);
        Registry::get('rdm')->setExpire('menu:'.session_id().':rule',86400);    //设置过期时间为2天，避免冗余
        $this->getView()->assign('menu',$menu);
        $this->getView()->display('leftMenu.phtml');
    }
}
